package toubiao.utils;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import toubiao.domain.po.business.PoProject;
import toubiao.utils.excel.domain.ExcelModel;
import toubiao.utils.excel.domain.ExcelModelColumn;
import toubiao.utils.filter.Convert;
import toubiao.utils.filter.HqlBuilder;
import toubiao.utils.filter.WebClass;
import toubiao.utils.filter.WebClassField;


    /**
    * @ClassName: Dictionary
    * @Description: 保存各种prop,xml 设置文件，以map形式保存
    * 
    * 1.CLASS_MAP 
    * 	<simpleName,fullName> like <ProjectGrid,toubiao.domain.vo.business.ProjectGrid>
    * 
    * 2.HQL_MAP
    * 	<hqlId	:	hlqString>	其中 hlqString 都为基本的Hql语句，即不包含 where 和 order 条件
    * 
    * 	a. 查询Po以及其属性的 实体类  Hql 语句 ，以 HqlId 以  List 结尾
    * 	hqlId= projectGridList
    *   hqlString=	select distinct project from PoProject project left join fetch project.contractSet contract left join fetch project.partA partA left join fetch project.snSet sn
    * 	
    * 	b.查询Po 数量的  Hql 语句 ，以 HqlId 以  Count 结尾
    * 	hqlId = projectGridCount
    * 	hqlString = select distinct project from PoProject project left join  project.contractSet contract left join  project.partA partA left join  project.snSet sn
    * 	注意上面没有fetch 
    * 
    * 3.HQL_CONVERT_MAP
    * 	<convertId,Convert> 
    * 	其中Convert为转换类：
    *   辅助将前台Hql筛选条件 Json 如  {"className":"project","classField":"designerNames","valueArray":["&联通"]} 
    *   className和classField 转换成 相应的 po 类型 简写,如上 实际属性为 PoProject.Designer.name; 最终转换成 designer.name 
    *   Hql 子条件转后为: designer.name like '%联通%'
    *   
    * 4.HQL_COMMON_CONVERT
    * 	一个id='common' 的Convert对象，即为上面得 	HQL_CONVERT_MAP.get("common")
    * 	通用的转换类,大部分情况下用该 Convert对象即可生成相应 Hql语句
    * 
    * 5.EXCEL_MODEL_MAP
    * 	<excelModelId,excelModel> 
    * 	其中excelModelId 为String,含义为 类名简写	;	excelModel 为 ExcelModel类型,记录怎么 将 Bean 对象 写入excel文件   
    * 
    * @author 宁少林
    * @date 2016年9月18日
    *
    */
public class Dictionary {
	
	static private Logger logger=LoggerFactory.getLogger(Dictionary.class);
	
	static public Map<String, String> HQL_MAP=new HashMap<>();
	static public Map<String, Convert> HQL_CONVERT_MAP=new HashMap<>();
	
	//<simpleName,fullName>
	//ProjectGrid=toubiao.domain.vo.business.ProjectGrid
	static public Map<String, String> CLASS_MAP=new HashMap<>();
	static private Map<String, String> classMap=new HashMap<>();//<simpleName,fullName> 其中simpleName均为小写，便于查询
	
	//build classMap
	static{
		String classMapFilesString=Dictionary.class.getResource("/config/classMap.properties").getPath();
		Properties properties=new Properties();
		try {
			FileInputStream fileInputStream=new FileInputStream(new File(classMapFilesString));
			properties.load(fileInputStream);
			properties.keySet();
			for(Object key:properties.keySet()){
				String simpleName=(String) key;
				String fullName=properties.getProperty(simpleName);
				CLASS_MAP.put(simpleName, fullName);
				classMap.put(simpleName.toLowerCase(), fullName);
			}
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException("初始化Class_Map 错误");
		}
	}
	
		
	static public String getFullClassName(String simpleClassName){
		return classMap.get(simpleClassName.toLowerCase());
	}
	
	//bulid hqlMap, hqlConvertMap
	static{
		String hqlFilePath=Dictionary.class.getResource("/config/hibernate/hql.xml").getPath();
		logger.info(hqlFilePath);
		SAXReader saxReader = new SAXReader();
		Document document=null;
		
		try {
			document = saxReader.read(new File(hqlFilePath));
		} catch (DocumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		Element root = document.getRootElement();
		System.out.println("Root: " + root.getName());
		
		//hqlList
		 List<Element> hqlList = root.element("hqlList").elements("hql");
		    for(Element hqlElement:hqlList){
		    	String id=hqlElement.attributeValue("id");
		    	String hql=hqlElement.getStringValue().trim();
		    	logger.info(id+" = "+hql);
		    	HQL_MAP.put(id, hql);
		    }
		
		//convertList
		List<Element> convertList = root.element("convertList").elements("convert");
		for(Element convertElement:convertList){
			Convert convert=new Convert();
			String id=convertElement.attributeValue("id");
			convert.setId(id);
			List<Element> webClassList=convertElement.elements("web-class");
	    	for(Element webClassElement:webClassList){
	    		WebClass webClass=new WebClass();
	    		String webClassName=webClassElement.attributeValue("name");
	    		webClass.setWebClassName(webClassName);
	    		List<Element> fieldList=webClassElement.elements("field");
	    		for(Element fieldElement:fieldList){
	    			String webClassFieldName=fieldElement.attributeValue("name");
	    			Element modelClass=fieldElement.element("model-class");
	    			Element modelField=fieldElement.element("model-field");
	    			String modelClassName=modelClass.attributeValue("name");
	    			String modelFieldName=modelField.attributeValue("name");
	    			String filterOperator=modelField.attributeValue("filter-operator");
	    			String type=modelField.attributeValue("type");
	    			WebClassField webClassField=new WebClassField(webClassName,webClassFieldName,modelClassName,modelFieldName,filterOperator,type);
	    			webClass.getFieldMap().put(webClassFieldName, webClassField);
	    		}
	    		convert.getWebClassMap().put(webClassName, webClass);
	    	}
	    	HQL_CONVERT_MAP.put(id, convert);
		}
	}
		
	static public Convert HQL_COMMON_CONVERT=HQL_CONVERT_MAP.get("common");
	
	static public Map<String, ExcelModel> EXCEL_MODEL_MAP=new HashMap<>();
	
	static{
		String excelModelFilePath=Dictionary.class.getResource("/config/excel/excelModel.xml").getPath();
		logger.info(excelModelFilePath);
		SAXReader saxReader = new SAXReader();
		Document document=null;
		
		try {
			document = saxReader.read(new File(excelModelFilePath));
		} catch (DocumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		Element root = document.getRootElement();
		System.out.println("Root: " + root.getName());
		
		 List<Element> excelModelList = root.element("excleModelList").elements("excelModel");
		 
		 for(Element excelModelElement:excelModelList){
			 ExcelModel excelModel=new ExcelModel();
			 String id=excelModelElement.attributeValue("id");
			 excelModel.setId(id);
			 List<Element> columnElementList=excelModelElement.elements("column");
			 for(Element columnElement:columnElementList){
				 ExcelModelColumn column=new ExcelModelColumn();
				 Integer number=Integer.valueOf(columnElement.attributeValue("number"));
				 String name=columnElement.attributeValue("name");
				 String fieldName=columnElement.attributeValue("filedName");
				 Integer width=Integer.parseInt(columnElement.attributeValue("width"));
				 column.setNumber(number);
				 column.setName(name);
				 column.setFiledName(fieldName);
				 column.setWidth(width);
				 excelModel.getFieldMap().put(name, column);
			 }
			 EXCEL_MODEL_MAP.put(id, excelModel);
		 }
	}
	
	
	
	/*static public Map<String, HqlBuilder> Hql=getHqlDictionary();
	private static Map<String, HqlBuilder> getHqlDictionary() {
		// TODO Auto-generated method stub
		Map<String, HqlBuilder> hqlDictionary=new HashMap<String, HqlBuilder>();
		String hqlFilePath=Dictionary.class.getResource("/config/hibernate/hql.xml").getPath();
		logger.info(hqlFilePath);
		SAXReader saxReader = new SAXReader();
		Document document=null;
		
		try {
			document = saxReader.read(new File(hqlFilePath));
		} catch (DocumentException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		Element root = document.getRootElement();
	    System.out.println("Root: " + root.getName());
	    // 获取所有子元素
	    List<Element> childList = root.elements();
	    System.out.println("total child count: " + childList.size());

	    // 获取特定名称的子元素
	    List<Element> childList2 = root.elements("web-class");
	    System.out.println("hello child: " + childList2.size());
	    
	    for(Element hqlElement:hqlList){
	    	String id=hqlElement.attributeValue("id");
	    	Element base=hqlElement.element("base");
	    	String hql=base.getStringValue().trim();
	    	HqlBuilder hqlBuilder=new HqlBuilder(id, hql);
	    	hqlDictionary.put(id, hqlBuilder);
	    	Element condition=hqlElement.element("condition");
	    	List<Element> webClassList=condition.elements("web-class");
	    	for(Element webClassElement:webClassList){
	    		WebClass webClass=new WebClass();
	    		String webClassName=webClassElement.attributeValue("name");
	    		hqlBuilder.getWebClassMap().put(webClassName, webClass);
	    		webClass.setWebClassName(webClassName);
	    		List<Element> fieldList=webClassElement.elements("field");
	    		for(Element fieldElement:fieldList){
	    			String webClassFieldName=fieldElement.attributeValue("name");
	    			Element modelClass=fieldElement.element("model-class");
	    			Element modelField=fieldElement.element("model-field");
	    			String modelClassName=modelClass.attributeValue("name");
	    			String modelFieldName=modelField.attributeValue("name");
	    			String filterOperator=modelField.attributeValue("filter-operator");
	    			String type=modelField.attributeValue("type");
	    			WebClassField webClassField=new WebClassField(webClassName,webClassFieldName,modelClassName,modelFieldName,filterOperator,type);
	    			webClass.getFieldMap().put(webClassFieldName, webClassField);
	    		}
	    	}
	    }
		return hqlDictionary;
	}*/
	
	
}
